package com.shenqi.parking.controller;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.shenqi.parking.service.IParkingSpotsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shenqi.parking.domain.ParkingApply;
import com.shenqi.parking.service.IParkingApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.security.authentication.AnonymousAuthenticationToken;

/**
 * 租赁意向Controller
 *
 * @author shenqi
 * @date 2025-07-27
 */
@RestController
@RequestMapping("/parking/apply")
public class ParkingApplyController extends BaseController {
    @Autowired
    private IParkingApplyService parkingApplyService;
    @Autowired
    private IParkingSpotsService parkingSpotsService;

    @PostMapping("sendMyApply")
    public AjaxResult sendMyApply(@RequestBody ParkingApply parkingApply) {
        String myUsername = SecurityUtils.getUsername();
        // 如果已经存在意向不可以新增
        ParkingApply query = new ParkingApply();
        query.setCreateBy(myUsername);
        query.setSpotId(parkingApply.getSpotId());
        List<ParkingApply> existApplys = parkingApplyService.selectParkingApplyList(query);
        if (existApplys.size() > 0) {
            return AjaxResult.error("你已经对这个车位发送过意向，不能再发起哈。可以去修改一下。");
        }
        parkingApply.setCreateBy(myUsername);
        parkingApplyService.insertParkingApply(parkingApply);
        return AjaxResult.success();
    }

    /**
     * 删除租赁意向
     */
    @DeleteMapping("/mine/{ids}")
    public AjaxResult removeMine(@PathVariable Long[] ids) {
        return toAjax(parkingApplyService.deleteMyParkingApplyByIds(ids));
    }

    @PutMapping("/mine")
    public AjaxResult editMine(@RequestBody ParkingApply parkingApply) {
        ParkingApply apply = parkingApplyService.selectParkingApplyById(parkingApply.getId());
        if(!apply.getCreateBy().equals(SecurityUtils.getUsername())){
            return AjaxResult.error("不能更新非自己的数据");
        }
        return toAjax(parkingApplyService.updateParkingApply(parkingApply));
    }

    @Anonymous
    @GetMapping("getMyApplyBySpotId")
    public AjaxResult getMyApplyBySpotId(@RequestParam Long spotId) {
        if(SecurityUtils.getAuthentication() instanceof AnonymousAuthenticationToken){
            return AjaxResult.success(null);
        }
        String myUsername = SecurityUtils.getUsername();
        ParkingApply query = new ParkingApply();
        query.setCreateBy(myUsername);
        query.setSpotId(spotId);
        List<ParkingApply> applies = parkingApplyService.selectParkingApplyList(query);
        return AjaxResult.success(applies.size() > 0 ? applies.get(0) : null);
    }

    /**
     * 查询我发出的意向
     * @return
     */
    @GetMapping("listSendApply")
    public TableDataInfo listSendApply() {
        ParkingApply parkingApply = new ParkingApply();
        String myUsername = SecurityUtils.getUsername();
        parkingApply.setCreateBy(myUsername);
        startPage();
        List<ParkingApply> list = parkingApplyService.selectParkingApplyList(parkingApply);
        return getDataTable(list);
    }

    /**
     * 查询我收到的意向
     * @return
     */
    @GetMapping("listReceivedApply")
    public TableDataInfo listReceivedApply() {
        String myUsername = SecurityUtils.getUsername();
        startPage();
        List<ParkingApply> list = parkingApplyService.selectParkingReceivedApplyList(myUsername);
        return getDataTable(list);
    }

    /**
     * 查询租赁意向列表
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(ParkingApply parkingApply) {
        startPage();
        List<ParkingApply> list = parkingApplyService.selectParkingApplyList(parkingApply);
        return getDataTable(list);
    }

    /**
     * 导出租赁意向列表
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:export')")
    @Log(title = "租赁意向", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParkingApply parkingApply) {
        List<ParkingApply> list = parkingApplyService.selectParkingApplyList(parkingApply);
        ExcelUtil<ParkingApply> util = new ExcelUtil<ParkingApply>(ParkingApply.class);
        util.exportExcel(response, list, "租赁意向数据");
    }

    /**
     * 获取租赁意向详细信息
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(parkingApplyService.selectParkingApplyById(id));
    }

    /**
     * 新增租赁意向
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:add')")
    @Log(title = "租赁意向", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkingApply parkingApply) {
        return toAjax(parkingApplyService.insertParkingApply(parkingApply));
    }

    /**
     * 修改租赁意向
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:edit')")
    @Log(title = "租赁意向", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkingApply parkingApply) {
        return toAjax(parkingApplyService.updateParkingApply(parkingApply));
    }

    /**
     * 删除租赁意向
     */
    @PreAuthorize("@ss.hasPermi('parking:apply:remove')")
    @Log(title = "租赁意向", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(parkingApplyService.deleteParkingApplyByIds(ids));
    }
}
